package spar.cancer.data.from.ecxt;

import redis.clients.jedis.Jedis;
import spar.cancer.Cancer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EcxtRedis extends EcxtAbstract {
    private final Jedis jedis;
    private final List<String> varsName = new ArrayList<>();

    public EcxtRedis( String host, int port) {
        jedis = new Jedis(host, port);
    }

    public EcxtRedis(List<Cancer<Double>> objs, String host, int port) {
        this(host, port);
        bind(objs);
    }

    public void close() {
        jedis.close();
    }

    @Override
    public void bind(List<Cancer<Double>> objs) {
        Cancer.setECxtObjs(objs);
        objs.forEach(obj -> {
            varsName.add(obj.getName());
        });
    }

    @Override
    public Map<String, Double> getNextData() {
        Map<String, Double> ret = new HashMap<>();
        for (String var : varsName) {
            Double val = Double.valueOf(jedis.get(var));
            ret.put(var, val);
        }
        return ret;
    }

    @Override
    public boolean hasNextData() {
        iterExit();
        boolean hasNextDataFlag = !Boolean.parseBoolean(jedis.get("exit"));
        iterEntry(hasNextDataFlag);
        return hasNextDataFlag;
    }
}
